/*
 * @Author: Snitro
 * @Date: 2021-05-17 20:26:59
 * @LastEditors: Snitro
 * @LastEditTime: 2021-05-22 14:54:06
 * @Description: file content
 */
#include "OLED_Operator.h"

uint8_t data_changed = 2;
uint8_t page_num = 0;

void data_Changed() {
    if (!data_changed) data_changed = 1;
}

void show_page() {
    if (page_num == 0)
        refresh_settings_page();
    else if (page_num == 1)
        refresh_data_page();
    else if (page_num == 2)
        refresh_page1();
    else
        refresh_page2();
}

void Operator_init() {
    oled_init();
    show_page();
}

int8_t arrow_keys_state = 0;

void key_press(int8_t key) {
    if (page_num == 3 && key == -1) {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
        arrow_keys_state = 1;
    } else if (page_num == 3 && key == -2) {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
        arrow_keys_state = 0;
    } else if (key <= -5) {
        if (key == -5) {
            if (arrow_keys_state)
                add_left_pos(-1000);
            else {
                if (page_num == 0)
                    page_num = 3;
                else
                    page_num--;
            }
        } else if (key == -6) {
            if (arrow_keys_state)
                add_left_pos(1000);
            else {
                page_num++;
                if (page_num > 3) page_num = 0;
            }
        }

        if (page_num && data_changed) {
            if (data_changed == 1)
                simple_tester();
            else
                tester();

            data_changed = 0;
            reset_pos();
        }
    } else if (key <= -3) {
        if (key == -3) {
            if (page_num == 0)
                data_page_press_key_up();
            else if (page_num == 3)
                add_bottom_pos(200);
        } else if (key == -4) {
            if (page_num == 0)
                data_page_press_key_down();
            else if (page_num == 3)
                add_bottom_pos(-200);
        }
    } else {
        if (page_num == 0) data_page_press_key(key);
        if (page_num == 3) {
            if (key == 5)
                reset_pos();
            else if (key == 6)
                add_xline_pos(1);
            else if (key == 4)
                add_xline_pos(-1);
            else if (key == 2)
                add_yline_pos(1);
            else if (key == 8)
                add_yline_pos(-1);
        }
    }

    show_page();
}

void key_keep_press(int8_t key) {
    if (key <= -5) {
        if (key == -5) {
            if (arrow_keys_state) add_left_pos(-10000);
        } else if (key == -6) {
            if (arrow_keys_state) add_left_pos(10000);
        }
    } else if (key <= -3) {
        if (key == -3) {
            if (page_num == 0)
                data_page_press_key_up();
            else if (page_num == 3)
                add_bottom_pos(350);
        } else if (key == -4) {
            if (page_num == 0)
                data_page_press_key_down();
            else if (page_num == 3)
                add_bottom_pos(-350);
        }
    } else {
        if (!key) {
            tester();
            data_changed = 0;
            reset_pos();
        } else if (page_num == 3) {
            if (key == 6)
                add_xline_pos(1);
            else if (key == 4)
                add_xline_pos(-1);
            else if (key == 2)
                add_yline_pos(1);
            else if (key == 8)
                add_yline_pos(-1);
        }
    }

    show_page();
}